package com.ruoyi.follow.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.follow.domain.TbFollow;
import com.ruoyi.follow.service.ITbFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 关注Controller
 *
 * @author 劳雅玲
 * @date 2024-03-01
 */
@RestController
@RequestMapping("/follow/follow")
public class TbFollowController extends BaseController {
    @Autowired
    private ITbFollowService tbFollowService;

    /**
     * 查询关注列表
     */
    @PreAuthorize("@ss.hasPermi('follow:follow:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbFollow tbFollow) {
//        System.out.println("啊啊啊啊啊"+tbFollow.toString());
        startPage();
        List<TbFollow> list = tbFollowService.selectTbFollowList(tbFollow);
        for (TbFollow t : list) {
//            System.out.println("啊啊啊啊啊"+t);
        }

        return getDataTable(list);
    }

    /**
     * 导出关注列表
     */
    @PreAuthorize("@ss.hasPermi('follow:follow:export')")
    @Log(title = "关注", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbFollow tbFollow) {
        List<TbFollow> list = tbFollowService.selectTbFollowList(tbFollow);
        ExcelUtil<TbFollow> util = new ExcelUtil<TbFollow>(TbFollow.class);
        util.exportExcel(response, list, "关注数据");
    }

    /**
     * 获取关注详细信息
     */
    @PreAuthorize("@ss.hasPermi('follow:follow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(tbFollowService.selectTbFollowById(id));
    }

    /**
     * 新增关注
     */
    @PreAuthorize("@ss.hasPermi('follow:follow:add')")
    @Log(title = "关注", businessType = BusinessType.INSERT)
    @PostMapping
    //传给前端的数据
    public AjaxResult add(@RequestBody TbFollow tbFollow) {
        return toAjax(tbFollowService.insertTbFollow(tbFollow));
    }


    /**
     * 关注/取消关注
     */
    @PreAuthorize("@ss.hasPermi('follow:follow:add')")
    @Log(title = "关注", businessType = BusinessType.INSERT)
    @PostMapping("/addOrCancel")
    //传给前端的数据
    public AjaxResult addOrCancel(@RequestBody TbFollow tbFollow) {
        return toAjax(tbFollowService.addOrCancel(tbFollow));
    }

    /**
     * 修改关注
     */
    @PreAuthorize("@ss.hasPermi('follow:follow:edit')")
    @Log(title = "关注", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbFollow tbFollow) {
        return toAjax(tbFollowService.updateTbFollow(tbFollow));
    }

    /**
     * 删除关注
     */
    @PreAuthorize("@ss.hasPermi('follow:follow:remove')")
    @Log(title = "关注", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tbFollowService.deleteTbFollowByIds(ids, getUserId()));
    }
}
